2 # Copyright (C) 2004 Brion Vibber <brion@pobox.com>
3 # http://www.mediawiki.org/
5 # This program is free software; you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 2 of the License, or
8 # (at your option) any later version.
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
15 # You should have received a copy of the GNU General Public License along
16 # with this program; if not, write to the Free Software Foundation, Inc.,
17 # 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18 # http://www.gnu.org/copyleft/gpl.html
21 * Run text & title search and display the output
23 * @subpackage SpecialPage
27 require_once( 'SearchEngine.php' );
28 require_once( 'Revision.php' );
33 * @param string $par (default '')
35 function wfSpecialSearch( $par = '' ) {
36 global $wgRequest, $wgUser;
38 $search = $wgRequest->getText( 'search', $par );
39 $searchPage = new SpecialSearch( $wgRequest, $wgUser );
40 if( $wgRequest->getVal( 'fulltext' ) ||
41 !is_null( $wgRequest->getVal( 'offset' ) ) ||
42 !is_null ($wgRequest->getVal( 'searchx' ) ) ) {
43 $searchPage->showResults( $search );
45 $searchPage->goResult( $search );
52 * @subpackage SpecialPage
57 * Set up basic search parameters from the request and user settings.
58 * Typically you'll pass $wgRequest and $wgUser.
60 * @param WebRequest $request
64 function SpecialSearch( &$request, &$user ) {
65 list( $this->limit
, $this->offset
) = $request->getLimitOffset( 20, 'searchlimit' );
67 if( $request->getCheck( 'searchx' ) ) {
68 $this->namespaces
= $this->powerSearch( $request );
70 $this->namespaces
= $this->userNamespaces( $user );
73 $this->searchRedirects
= false;
77 * If an exact title match can be found, jump straight ahead to
81 function goResult( $term ) {
85 $this->setupPage( $term );
87 # Try to go to page as entered.
89 $t = Title
::newFromText( $term );
91 # If the string cannot be used to create a title
93 return $this->showResults( $term );
96 # If there's an exact or very near match, jump right there.
97 $t = SearchEngine
::getNearMatch( $term );
98 if( !is_null( $t ) ) {
99 $wgOut->redirect( $t->getFullURL() );
103 # No match, generate an edit URL
104 $t = Title
::newFromText( $term );
105 if( is_null( $t ) ) {
106 $editurl = ''; # hrm...
108 # If the feature is enabled, go straight to the edit page
110 $wgOut->redirect( $t->getFullURL( 'action=edit' ) );
113 $editurl = $t->escapeLocalURL( 'action=edit' );
116 $wgOut->addWikiText( wfMsg('nogomatch', ":$term" ) );
118 return $this->showResults( $term );
122 * @param string $term
125 function showResults( $term ) {
126 $fname = 'SpecialSearch::showResults';
127 wfProfileIn( $fname );
129 $this->setupPage( $term );
131 global $wgUser, $wgOut;
132 $sk = $wgUser->getSkin();
133 $wgOut->addWikiText( wfMsg( 'searchresulttext' ) );
135 #if ( !$this->parseQuery() ) {
136 if( '' === trim( $term ) ) {
137 $wgOut->setSubtitle( '' );
138 $wgOut->addHTML( $this->powerSearchBox( $term ) );
139 wfProfileOut( $fname );
143 global $wgDisableTextSearch;
144 if ( $wgDisableTextSearch ) {
145 global $wgForwardSearchUrl;
146 if( $wgForwardSearchUrl ) {
147 $url = str_replace( '$1', urlencode( $term ), $wgForwardSearchUrl );
148 $wgOut->redirect( $url );
151 global $wgInputEncoding;
152 $wgOut->addHTML( wfMsg( 'searchdisabled' ) );
154 wfMsg( 'googlesearch',
155 htmlspecialchars( $term ),
156 htmlspecialchars( $wgInputEncoding ),
157 htmlspecialchars( wfMsg( 'search' ) )
160 wfProfileOut( $fname );
164 $search = SearchEngine
::create();
165 $search->setLimitOffset( $this->limit
, $this->offset
);
166 $search->setNamespaces( $this->namespaces
);
167 $titleMatches = $search->searchTitle( $term );
168 $textMatches = $search->searchText( $term );
170 $num = ( $titleMatches ?
$titleMatches->numRows() : 0 )
171 +
( $textMatches ?
$textMatches->numRows() : 0);
172 if ( $num >= $this->limit
) {
173 $top = wfShowingResults( $this->offset
, $this->limit
);
175 $top = wfShowingResultsNum( $this->offset
, $this->limit
, $num );
177 $wgOut->addHTML( "<p>{$top}</p>\n" );
179 if( $num ||
$this->offset
) {
180 $prevnext = wfViewPrevNext( $this->offset
, $this->limit
,
183 $this->powerSearchOptions(),
184 array( 'search' => $term ) ) );
185 $wgOut->addHTML( "<br />{$prevnext}\n" );
188 if( $titleMatches ) {
189 if( $titleMatches->numRows() ) {
190 $wgOut->addWikiText( '==' . wfMsg( 'titlematches' ) . "==\n" );
191 $wgOut->addHTML( $this->showMatches( $titleMatches ) );
193 $wgOut->addWikiText( '==' . wfMsg( 'notitlematches' ) . "==\n" );
198 if( $textMatches->numRows() ) {
199 $wgOut->addWikiText( '==' . wfMsg( 'textmatches' ) . "==\n" );
200 $wgOut->addHTML( $this->showMatches( $textMatches ) );
201 } elseif( $num == 0 ) {
202 # Don't show the 'no text matches' if we received title matches
203 $wgOut->addWikiText( '==' . wfMsg( 'notextmatches' ) . "==\n" );
208 $wgOut->addWikiText( wfMsg( 'nonefound' ) );
210 if( $num ||
$this->offset
) {
211 $wgOut->addHTML( "<p>{$prevnext}</p>\n" );
213 $wgOut->addHTML( $this->powerSearchBox( $term ) );
214 wfProfileOut( $fname );
217 #------------------------------------------------------------------
218 # Private methods below this line
223 function setupPage( $term ) {
225 $wgOut->setPageTitle( wfMsg( 'searchresults' ) );
226 $wgOut->setSubtitle( wfMsg( 'searchquery', $term ) );
227 $wgOut->setArticleRelated( false );
228 $wgOut->setRobotpolicy( 'noindex,nofollow' );
232 * Extract default namespaces to search from the given user's
233 * settings, returning a list of index numbers.
239 function userNamespaces( &$user ) {
241 foreach( SearchEngine
::searchableNamespaces() as $ns => $name ) {
242 if( $user->getOption( 'searchNs' . $ns ) ) {
250 * Extract "power search" namespace settings from the request object,
251 * returning a list of index numbers to search.
253 * @param WebRequest $request
257 function powerSearch( &$request ) {
259 foreach( SearchEngine
::searchableNamespaces() as $ns => $name ) {
260 if( $request->getCheck( 'ns' . $ns ) ) {
268 * Reconstruct the 'power search' options for links
272 function powerSearchOptions() {
274 foreach( $this->namespaces
as $n ) {
277 $opt['redirs'] = $this->searchRedirects ?
1 : 0;
283 * @param SearchResultSet $matches
284 * @param string $terms partial regexp for highlighting terms
286 function showMatches( &$matches ) {
287 $fname = 'SpecialSearch::showMatches';
288 wfProfileIn( $fname );
291 $tm = $wgContLang->convertForSearchResult( $matches->termMatches() );
292 $terms = implode( '|', $tm );
295 $off = $this->offset +
1;
296 $out = "<ol start='{$off}'>\n";
298 while( $result = $matches->next() ) {
299 $out .= $this->showHit( $result, $terms );
303 // convert the whole thing to desired language variant
305 $out = $wgContLang->convert( $out );
306 wfProfileOut( $fname );
311 * Format a single hit result
312 * @param SearchResult $result
313 * @param string $terms partial regexp for highlighting terms
315 function showHit( $result, $terms ) {
316 $fname = 'SpecialSearch::showHit';
317 wfProfileIn( $fname );
318 global $wgUser, $wgContLang;
320 $t = $result->getTitle();
321 if( is_null( $t ) ) {
322 wfProfileOut( $fname );
323 return "<!-- Broken link in search result -->\n";
325 $sk =& $wgUser->getSkin();
327 $contextlines = $wgUser->getOption( 'contextlines' );
328 if ( '' == $contextlines ) { $contextlines = 5; }
329 $contextchars = $wgUser->getOption( 'contextchars' );
330 if ( '' == $contextchars ) { $contextchars = 50; }
332 $link = $sk->makeKnownLinkObj( $t );
333 $revision = Revision
::newFromTitle( $t );
334 $text = $revision->getText();
335 $size = wfMsg( 'nbytes', strlen( $text ) );
337 $lines = explode( "\n", $text );
339 $max = IntVal( $contextchars ) +
1;
340 $pat1 = "/(.*)($terms)(.{0,$max})/i";
345 wfProfileIn( "$fname-extract" );
346 foreach ( $lines as $line ) {
347 if ( 0 == $contextlines ) {
351 if ( ! preg_match( $pat1, $line, $m ) ) {
355 $pre = $wgContLang->truncate( $m[1], -$contextchars, '...' );
357 if ( count( $m ) < 3 ) {
360 $post = $wgContLang->truncate( $m[3], $contextchars, '...' );
365 $line = htmlspecialchars( $pre . $found . $post );
366 $pat2 = '/(' . $terms . ")/i";
367 $line = preg_replace( $pat2,
368 "<span class='searchmatch'>\\1</span>", $line );
370 $extract .= "<br /><small>{$lineno}: {$line}</small>\n";
372 wfProfileOut( "$fname-extract" );
373 wfProfileOut( $fname );
374 return "<li>{$link} ({$size}){$extract}</li>\n";
377 function powerSearchBox( $term ) {
379 foreach( SearchEngine
::searchableNamespaces() as $ns => $name ) {
380 $checked = in_array( $ns, $this->namespaces
)
381 ?
' checked="checked"'
383 $name = str_replace( '_', ' ', $name );
385 $name = wfMsg( 'blanknamespace' );
387 $namespaces .= " <label><input type='checkbox' value=\"1\" name=\"" .
388 "ns{$ns}\"{$checked} />{$name}</label>\n";
391 $checked = $this->searchRedirects
392 ?
' checked="checked"'
394 $redirect = "<input type='checkbox' value='1' name=\"redirs\"{$checked} />\n";
396 $searchField = "<input type='text' name=\"search\" value=\"" .
397 htmlspecialchars( $term ) ."\" width=\"80\" />\n";
399 $searchButton = '<input type="submit" name="searchx" value="' .
400 htmlspecialchars( wfMsg('powersearch') ) . "\" />\n";
402 $ret = wfMsg( 'powersearchtext',
403 $namespaces, $redirect, $searchField,
404 '', '', '', '', '', # Dummy placeholders
407 $title = Title
::makeTitle( NS_SPECIAL
, 'Search' );
408 $action = $title->escapeLocalURL();
409 return "<br /><br />\n<form id=\"powersearch\" method=\"get\" " .
410 "action=\"$action\">\n{$ret}\n</form>\n";